/************************************************************
** @Description: FYKJ_Admin
** @Author: dabin
** @Date:   2021-03-02 21:21
** @Last Modified by:   dabin
** @Last Modified time: 2021-03-02 21:21
*************************************************************/
package login

import (
	"FYKJ_Admin/models/auth"
	_ "FYKJ_Admin/models/auth"
	"FYKJ_Admin/utils"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	_ "github.com/mojocn/base64Captcha"
)

//定义登入的结构体
type LoginController struct {
	beego.Controller
}

//结构体调用
func (l *LoginController) Get() {
	//函数调用
	id, base64, err := utils.GetCaptcha()
	if err != nil {
		//错误日志信息收集
		ret :=fmt.Sprintf("登录get请求,获取验证码错误，错误信息：%v",err)
		logs.Error(ret)
		return
	}
	//结构体调用
	l.Data["captcha"] = utils.Captcha{Id: id, BS64: base64}
	l.TplName = "login/login.html"
}

func (l *LoginController) Post() {
	username := l.GetString("username")
	password := l.GetString("password")
	captcha := l.GetString("captcha")
	captcha_id := l.GetString("captcha_id")
	//fmt.Println(username)
	//fmt.Println(password)

	//md5获取
	md5_pwd := utils.GetMd5File(password)

	userinfo := auth.User{}
	o := orm.NewOrm()
	is_exist := o.QueryTable("sys_user").Filter("user_name", username).Filter("pass_word", md5_pwd).Exist()
	//fmt.Println(user)
	//fmt.Println(is_exist)
	o.QueryTable("sys_user").Filter("user_name", username).Filter("pass_word", md5_pwd).One(&userinfo)
	//验证码校验
	is_ok := utils.VerityCaptcha(captcha_id, captcha)
	fmt.Println(is_ok)

	ret_map := map[string]interface{}{}
	if !is_exist {
		ret :=fmt.Sprintf("登录post请求,用户名或密码错误，登录信息：username：%s;password:%s",username,md5_pwd)
		logs.Info(ret)
		ret_map["code"] = 10001
		ret_map["msg"] = "用户名或密码错误！"
		l.Data["json"] = ret_map
	} else if !is_ok {
		ret :=fmt.Sprintf("登录post请求,验证码错误，验证码信息：%t",is_ok)
		logs.Info(ret)
		ret_map["code"] = 10001
		ret_map["msg"] = "验证码错误！"
		l.Data["json"] = ret_map
	} else if userinfo.IsActive == 2 {
		ret :=fmt.Sprintf("登录post请求,该用户已停用，用户名：%s,状态:停用",username)
		logs.Info(ret)
		ret_map["code"] = 10001
		ret_map["msg"] = "该用户已停用，请联系管理员！"
		l.Data["json"] = ret_map
	}else {
		ret :=fmt.Sprintf("登录post请求,登录成功，登录信息：username：%s;password:%s",username,md5_pwd)
		logs.Info(ret)
		//o.QueryTable("sys_user").Filter("user_name", username).Filter("pass_word", md5_pwd).One(&userinfo)
		l.SetSession("id", userinfo.SysId)
		//fmt.Println(user)
		ret_map["code"] = 200
		ret_map["msg"] = "登录成功！"
		l.Data["json"] = ret_map
	}
	l.ServeJSON()
}

func (l *LoginController) ChangeCaptcha() {
	message := map[string]interface{}{}
	//函数调用
	id, base64, err := utils.GetCaptcha()
	if err != nil { //说明有错误
		ret :=fmt.Sprintf("生成验证码失败，验证码信息：id:%s;错误信息:%v",id,err)
		logs.Info(ret)
		message["msg"] = "生成验证码失败！"
		message["Code"] = 404
		l.Data["json"] = message
	} else {
		l.Data["json"] = utils.Captcha{Id: id, BS64: base64, Code: 200}
	}

	l.ServeJSON()
}


func (l *LoginController) LogOut()  {
	//登入的时候获取的用户id内存存放清除
	l.DelSession("id")
	l.Redirect(beego.URLFor("LoginController.Get"),302)

}

